<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/25
 * Time: 17:34
 */

namespace app\common\logic;

class Tag extends Base
{
    /**
     * 检查电子标签是否已经被使用
     * @param $tag_id
     * @param int $type 绑定的对象类型，1：资产，2：包装箱
     * @return bool | string
     */
    public function checkIsUsed($tag_id, $type = 1)
    {
        if (empty($tag_id)) {
            return false;
        }

        $assets_model = model('Assets');
        $assets_pack_model = model('AssetsPack');

        //分别从资产表和包装箱表中寻找是否存在重复的电子标签
        if ($type == 1) {
            //优先从资产表中寻找
            $asset_info = $assets_model->getInfo(['tag_id' => $tag_id], 'tag_id');
            if (!empty($asset_info)) {
                return true;
            }

            $pack_info = $assets_pack_model->getInfo(['tag_id' => $tag_id], 'tag_id');
            if (!empty($pack_info)) {
                return true;
            }
        } else {
            //优先从包装箱表中寻找
            $asset_info = $assets_model->getInfo(['tag_id' => $tag_id], 'tag_id');
            if (!empty($asset_info)) {
                return true;
            }

            $pack_info = $assets_pack_model->getInfo(['tag_id' => $tag_id], 'tag_id');
            if (!empty($pack_info)) {
                return true;
            }
        }

        return false;
    }


}